package com.cpi.cornpp.user.dao;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import KC.ModSegApp.AplicacionApp;
import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.user.dto.UserDTO;
import com.cpi.cornpp.user.exception.UserException;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;

/**
 * 
 * @description: This Class is used to handle the User Data Access
 * @author Edgar Joao
 */

public class UserDAO extends DBDAO{
	/** User Datasource */
	private static final String DATASOURCE 	= "modseg";//$NON-NLS-1$	
	/**Application Module */
	private static final String MOD_APPLICATION = "MOD_SEG_APPLICATION";//$NON-NLS-1$
	/**Error invalid login/pass key */
	private static final String ERROR_INVALID_LOGIN_PASSWORD = "error.authentication.usertoappinvalid";//$NON-NLS-1$
	/**Error invalid login/app key */
	private static final String ERROR_INVALID_LOGIN_FOR_APP = "error.authentication.useropassinvalid";//$NON-NLS-1$
	/** Object for doing logs */
	private static org.apache.log4j.Logger logger = Logger.getLogger(UserDAO.class);
    /**
     * Login User method	
     * @param userDTO
     * @return UsuarioApp
     * @throws UserException
     */
	public UsuarioApp loginUser(UserDTO userDTO) throws UserException {
		Connection connection = null;
		UsuarioApp usuario = null;
		AplicacionApp aplicacion = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		String applicationName = null;
		try {
			connection = this.getConnection(DATASOURCE);
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(MOD_APPLICATION);
			applicationName = systemParameterBean.getValue();
			usuario = UsuarioApp.usuarioValido(connection, userDTO.getUsrId(), userDTO.getPassword());
			if (null!=usuario) {
				aplicacion = AplicacionApp.aplicacionValida(connection, userDTO.getUsrId(), applicationName);
				if (null!=aplicacion) {
					usuario.setAplicacionAValidar(aplicacion);
				}else{
					UserException userException = null;
					userException = new UserException(ERROR_INVALID_LOGIN_PASSWORD,CornPPException.LAYER_DAO, CornPPException.ACTION_LOGIN);
					throw userException;
				}
			}else{
				UserException userException = null;
				userException = new UserException(ERROR_INVALID_LOGIN_FOR_APP,CornPPException.LAYER_DAO, CornPPException.ACTION_LOGIN);
				throw userException;
			}
		} catch (UserException userException){
			logger.error(userException);
			throw userException;
		} catch( PoolException poolException){
			UserException userException = null;
			logger.error(poolException);
			poolException.printStackTrace(System.err);
			userException = new UserException(poolException,CornPPException.LAYER_DAO, CornPPException.ACTION_LOGIN);
			throw userException;
		} catch (SQLException sqlException) {
			UserException userException = null;
			logger.error(sqlException);
			sqlException.printStackTrace(System.err);
			userException = new UserException(sqlException,CornPPException.LAYER_DAO, CornPPException.ACTION_LOGIN);
			throw userException;
		} catch (Exception exception) {
			UserException userException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			userException = new UserException(exception,CornPPException.LAYER_DAO, CornPPException.ACTION_LOGIN);
			throw userException;
		}finally{
			try{
				closeConnection(connection);
			}catch(SQLException sqlException){
				logger.error(sqlException);
				sqlException.printStackTrace(System.err);
			}
		}
		return usuario;
	}
}